#include "menuwindow.h"

MenuWindow::MenuWindow(QWidget *parent) : QMainWindow(parent)
{
    //给菜单栏添加菜单
    QMenuBar* pMenuBar = this->menuBar();
    //    this->resize(300,500);
    this->setWindowTitle("MenuBar");
    QMenu* pFileMenu = pMenuBar->addMenu("File");

    //给菜单栏添加菜单项功能
    QAction* pActionAsSave = pFileMenu->addAction("Save as");
    pFileMenu->addSeparator();
    QAction* pActionAsSaveAll = pFileMenu->addAction("Save All Menu");

    //给菜单绑定事件
    connect(pActionAsSave,&QAction::triggered,
            [=](){
        qDebug()<<"Save as clicked";
    });

    connect(pActionAsSaveAll,&QAction::triggered,
            [=](){
        qDebug()<<"Save All clicked";
    });
    QAction* pFile = pFileMenu->addAction("打开文件");
    QAction* pModal = pFileMenu->addAction("弹出模态对话框");
    QAction* pModeless = pFileMenu->addAction("弹出非模态对话框");
    QAction* pMessageBox = pFileMenu->addAction("弹出MessageBox");

    connect(pModal,&QAction::triggered,
            [=](){
        //        模态对话框
        QDialog qDialog;
        qDialog.exec();
    });

    connect(pModeless,&QAction::triggered,
            [=](){
        //非模态对话框
        QDialog* qDialog = new QDialog;
        //关闭窗口时释放对象
        qDialog->setAttribute(Qt::WA_DeleteOnClose);
        qDialog->show();

    });

    connect(pFile,&QAction::triggered,
            [=](){

        QString qString = QFileDialog::getOpenFileName(
                    this
                    ,tr("Open Image")
                    ,"C:\\Users\\Administrator\\Desktop\\JavaCode"
                    ,"*.*");
        qDebug()<<qString;
        QString qStringTr = QFileDialog::getOpenFileName(
                    this,
                    tr("Open Image"),
                    ".",
                    tr("Image Files (*.png *.jpg *.bmp)"));
        qDebug()<<qStringTr;
        QString fileName = QFileDialog::getSaveFileName(
                    this,
                    tr("Save File"),
                    "./",
                    tr("Images (*.png *.xpm *.jpg);;"
                       "Text(*.txt);;"
                       "Execute(*.exe *.dll *.sys);;"
                       "all(*.*)"));

        qDebug()<<fileName;
    });


    connect(pMessageBox,&QAction::triggered,
            [=](){

        // QMessageBox::about(this,"title","content");
        //        QMessageBox::aboutQt(this,"title");


        //                QMessageBox msgBox;
        //                 msgBox.setText("The document has been modified.");
        //                 msgBox.exec();
        //自定义图标 question ?,information !,warning !
        int ret = QMessageBox::warning(
                    this, tr("My Application"),
                    tr("The document has been modified.\n"
                       "Do you want to save your changes?"),
                    QMessageBox::Ok |  QMessageBox::Cancel,//显示哪些按钮
                    QMessageBox::Ok);//默认选中哪个按钮
        switch (ret) {
        case QMessageBox::Save:
            // Save was clicked
            break;
        case QMessageBox::Ok:
            //Ok was clicked
            break;
        case QMessageBox::Cancel:
            // Cancel was clicked
            break;
        default:
            // should never be reached
            break;
        }
        qDebug()<<"pModeless exec end!";

    });

    //--------------------------------------------------------
    //工具栏
    QToolBar* pToolBar = this->addToolBar("ToolBar");
    //添加菜单到工具栏
    pToolBar->addAction(pActionAsSave);

    //添加按钮到工具栏
    QPushButton* pButton = new QPushButton(pToolBar);
    pButton->setText("@_@");
    pToolBar->addWidget(pButton);
    //绑定按钮事件
    connect(pButton,&QPushButton::clicked,
            [=](){
        pButton->setText("Change");
        qDebug()<<"ChangeButtonText click";
    });

    //状态栏
    QStatusBar* pStatusBar = this->statusBar();
    QLabel* pLabel = new QLabel(pStatusBar);
    pLabel->setText("Label Text");
    pStatusBar->addWidget(pLabel);
    //从左往右添加菜单
    pStatusBar->addWidget(new QLabel("Hello add to left",pStatusBar));
    pStatusBar->addPermanentWidget(new QLabel("Hello add to right",pStatusBar));

    //文本域
    QTextEdit* ptextEdit = new QTextEdit(this);
    this->setCentralWidget(ptextEdit);


    //浮动窗口
    QDockWidget* pDock = new QDockWidget("dock",this);
    this->addDockWidget(Qt::BottomDockWidgetArea,pDock);
    QTextEdit* ptextEditDock = new QTextEdit(this);
    pDock->setWidget(ptextEditDock);

}
